package com.example.admin.goodnews.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.admin.goodnews.R;

/**
 * 二级页面
 * Created by admin on 2016/10/9.
 */

public class SecondaryPageActivity extends AppCompatActivity {
    private ImageView iv_back_secondarypage;// 标题栏返回按钮
    private WebView webview;// 加载页面的webview
    private TextView tv_title_secondarypage;// 标题文字
    // private ProgressDialog dialog;// 加载html时的进度对话框

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_secondarypage);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        iv_back_secondarypage = (ImageView) findViewById(R.id.iv_back_secondarypage);// 标题栏返回按钮
        webview = (WebView) findViewById(R.id.webview);// 加载页面的webview
        tv_title_secondarypage = (TextView) findViewById(R.id.tv_title_secondarypage);// 标题文字
    }

    private void initData() {
        Intent intent = getIntent();
        final String link = intent.getStringExtra("link");
        final String title = intent.getStringExtra("title");
        tv_title_secondarypage.setText(title);// 设置标题文字
        WebSettings webSettings = webview.getSettings();
        webSettings.setPluginState(WebSettings.PluginState.ON);
        webSettings.setUseWideViewPort(true);
        webSettings.setJavaScriptEnabled(true);
        webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
//        webview.loadUrl(url);
        webview = (WebView) findViewById(R.id.webview);
        //WebView加载web资源
        webview.loadUrl(link);
        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (!webview.getSettings().getLoadsImagesAutomatically()) {
                    webview.getSettings().setLoadsImagesAutomatically(true);
                }
            }
        });

        // 设置加载时的进度条
        //        webview.setWebChromeClient(new WebChromeClient() {
        //            @Override
        //            public void onProgressChanged(WebView view, int newProgress) {
        //                if (newProgress == 100) {
        //                    closeDialog();
        //                } else {
        //                    openDialog(newProgress);
        //                }
        //            }
        //        });
    }

    // 关闭进度条
    //    private void closeDialog() {
    //        if (dialog != null && dialog.isShowing()) {
    //            dialog.dismiss();
    //            dialog = null;
    //        }
    //    }

    // 打开进度条，并设置进度条样式
    //    private void openDialog(int newProgress) {
    //        if (dialog == null) {
    //            dialog = new ProgressDialog(SecondaryPageActivity.this);
    //            dialog.setTitle("正在加载...");
    //            dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    //            dialog.setProgress(newProgress);
    //            dialog.show();
    //        } else {
    //            dialog.setProgress(newProgress);
    //        }
    //    }

    private void initEvent() {
        // 标题栏返回按钮
        iv_back_secondarypage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
